﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;
using AjaxControlToolkit;

public partial class QLVB_User_MauBieuChuaDuyet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao()
    {
        MauBieu mbDTO = new MauBieu();
        mbDTO.TenFileMauBieu = txtTenFileMauBieuCanTim.Text.ToUpper();
        mbDTO.MaNguoiSoanThao = (String)Session["MaNguoiDung"];

        MauBieuBUS mbBUS = new MauBieuBUS();
        gvDanhSachMauBieuChuaDuyet.DataSource = mbBUS.layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao(mbDTO);
        gvDanhSachMauBieuChuaDuyet.DataBind();
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao();
    }
    protected void gvDanhSachMauBieuChuaDuyet_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDanhSachMauBieuChuaDuyet.PageIndex = e.NewPageIndex;

        layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao();
    }
    protected void gvDanhSachMauBieuChuaDuyet_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDanhSachMauBieuChuaDuyet.EditIndex = e.NewEditIndex;

        layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao();
    }
    protected void gvDanhSachMauBieuChuaDuyet_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //
        TextBox txtNoiDungMauBieu = (TextBox)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("txtNoiDungMauBieu"));
        FileUpload fuplTenFileMauBieu = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("fuplTenFileMauBieu"));
        FileUpload fuplTenFileCongVan = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("fuplTenFileCongVan"));
        ComboBox cmbMaNoiPhatHanh = (ComboBox)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("cmbMaNoiPhatHanh"));
        TextBox txtTenDonViSoanThao = (TextBox)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("txtTenDonViSoanThao"));
        HiddenField hfTenFileMauBieu = (HiddenField)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("hfTenFileMauBieu"));
        HiddenField hfTenFileCongVan = (HiddenField)(gvDanhSachMauBieuChuaDuyet.Rows[e.RowIndex].Cells[1].FindControl("hfTenFileCongVan"));

        //Kiem tra
        if (txtNoiDungMauBieu.Text == string.Empty)
        {
            Alert.Show("Noi dung mau bieu khong duoc rong");

            return;
        }

        if (txtTenDonViSoanThao.Text == string.Empty)
        {
            Alert.Show("Ten don vi soan thao khong duoc rong");

            return;
        }

        //
        MauBieu mbDTO = new MauBieu();
        mbDTO.MaMauBieu = Convert.ToInt32(gvDanhSachMauBieuChuaDuyet.DataKeys[e.RowIndex].Value.ToString());
        mbDTO.NoiDungMauBieu = txtNoiDungMauBieu.Text;
        if (fuplTenFileMauBieu.PostedFile.FileName == string.Empty)
        {
            mbDTO.TenFileMauBieu = hfTenFileMauBieu.Value.ToString();
        }
        else
        {
            mbDTO.TenFileMauBieu = fuplTenFileMauBieu.PostedFile.FileName;
        }
        if (fuplTenFileCongVan.PostedFile.FileName == string.Empty)
        {
            mbDTO.TenFileCongVan = hfTenFileCongVan.Value.ToString();
        }
        else
        {
            mbDTO.TenFileCongVan = fuplTenFileCongVan.PostedFile.FileName;
        }
        mbDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue.ToString();
        mbDTO.TenDonViSoanThao = txtTenDonViSoanThao.Text;

        //Upload
        if (fuplTenFileMauBieu.PostedFile.FileName != string.Empty)
        {
            uploadFileMauBieu();
        }
        if (fuplTenFileCongVan.PostedFile.FileName != string.Empty)
        {
            uploadFileCongVan();
        }

        //Sua
        MauBieuBUS mbBUS = new MauBieuBUS();
        mbBUS.suaMauBieuChuaDuyet(mbDTO);

        Alert.Show("Da sua thanh cong");
        
        gvDanhSachMauBieuChuaDuyet.EditIndex = -1;

        layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao();
    }
    protected void gvDanhSachMauBieuChuaDuyet_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDanhSachMauBieuChuaDuyet.EditIndex = -1;

        layDanhSachMauBieuChuaDuyetTheoTenFileMauBieuVaMaNguoiSoanThao();
    }
    protected void gvDanhSachMauBieuChuaDuyet_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        ComboBox cmbMaNoiPhatHanh = (ComboBox)(e.Row.FindControl("cmbMaNoiPhatHanh"));

        if (cmbMaNoiPhatHanh != null)
        {
            MauBieu mbDTO = new MauBieu();
            mbDTO.MaMauBieu = Convert.ToInt32(gvDanhSachMauBieuChuaDuyet.DataKeys[e.Row.RowIndex].Value.ToString());

            MauBieuBUS mbBUS = new MauBieuBUS();
            IList<MauBieu> lstMauBieu = mbBUS.layThongTinMauBieuTheoMaMauBieu(mbDTO);
            foreach (MauBieu mb in lstMauBieu)
            {
                NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

                cmbMaNoiPhatHanh.DataSource = nphBUS.layDanhSachNoiPhatHanh();
                cmbMaNoiPhatHanh.DataTextField = "TenNoiPhatHanh";
                cmbMaNoiPhatHanh.DataValueField = "MaNoiPhatHanh";
                cmbMaNoiPhatHanh.SelectedValue = mb.MaNoiPhatHanh;
                cmbMaNoiPhatHanh.DataBind();
            }
        }
    }
    protected void cmbMaNoiPhatHanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        layThongTinChiTietMaNoiPhatHanh();
    }
    private void layThongTinChiTietMaNoiPhatHanh()
    {
        for (int i = 0; i < gvDanhSachMauBieuChuaDuyet.Rows.Count; i++)
        {
            TextBox txtNoiDungMauBieu = (TextBox)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("txtNoiDungMauBieu"));
            FileUpload fuplTenFileMauBieu = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("fuplTenFileMauBieu"));
            FileUpload fuplTenFileCongVan = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("fuplTenFileCongVan"));
            ComboBox cmbMaNoiPhatHanh = (ComboBox)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("cmbMaNoiPhatHanh"));
            TextBox txtTenDonViSoanThao = (TextBox)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("txtTenDonViSoanThao"));

            NoiPhatHanh nphDTO = new NoiPhatHanh();
            nphDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue;

            NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

            IList<NoiPhatHanh> lstThongTinNoiPhatHanh = nphBUS.layThongTinNoiPhatHanhTheoMaNoiPhatHanh(nphDTO);
            switch (nphDTO.MaNoiPhatHanh)
            {
                case "BIDV":
                    txtTenDonViSoanThao.Text = "BIDV";
                    break;
                case "BIDVCM":
                    txtTenDonViSoanThao.Text = "BIDV CAMAU";
                    break;
                case "PT":
                    txtTenDonViSoanThao.Text = "BIDV CAMAU";
                    break;
                default:
                    txtTenDonViSoanThao.Text = string.Empty;
                    break;
            }
        }
    }
    protected void uploadFileMauBieu()
    {
        for (int i = 0; i < gvDanhSachMauBieuChuaDuyet.Rows.Count; i++)
        {
            FileUpload fuplTenFileMauBieu = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("fuplTenFileMauBieu"));
            
            string tenFile = fuplTenFileMauBieu.PostedFile.FileName;

            string path = Server.MapPath("..//VanBanLuuTru/MauBieu");

            path += "/" + tenFile;

            fuplTenFileMauBieu.PostedFile.SaveAs(path);
        }
    }
    protected void uploadFileCongVan()
    {
        for (int i = 0; i < gvDanhSachMauBieuChuaDuyet.Rows.Count; i++)
        {
            FileUpload fuplTenFileCongVan = (FileUpload)(gvDanhSachMauBieuChuaDuyet.Rows[i].Cells[1].FindControl("fuplTenFileCongVan"));

            string tenFile = fuplTenFileCongVan.PostedFile.FileName;

            string path = Server.MapPath("..//VanBanLuuTru/MauBieu");

            path += "/" + tenFile;

            fuplTenFileCongVan.PostedFile.SaveAs(path);
        }
    }
}